ngx.header.content_type = "text/plain";

local redis = require "resty.redis";
local config = ngx.shared.config;
local instance = redis:new();
local pool_max_idle_time = 100000; --毫秒
local pool_size = 100; --连接池大小
local host = config:get("host");
local port = config:get("port");
local SYNC_TAB = config:get("SYNC_TAB");--vpn访问，播放记录缓存
local SYNC_DATACENTER = config:get("SYNC_DATACENTER");--公网访问，播放记录缓存

--连接redis
local ok, err = instance:connect(host, port);
--instance:set_keepalive(pool_max_idle_time, pool_size);
if not ok then
  ngx.log(ngx.ERR, err);
  ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE);
end

--json
local cjson = require "cjson";
local res={};
local _arrayFlagKey={};
_arrayFlagKey["msgCode"]=1;

--获取提交的数据
--GET get_uri_args;
--POST get_post_args;
local request_method = ngx.var.request_method;
local params = nil;
local params_arr = {};
local tab = nil;
local key = nil;
local status = nil;

if request_method == "GET"  then
  params = ngx.req.get_uri_args()['params'] or 0;
elseif request_method == "POST"  then  
  ngx.req.read_body();
  params = ngx.req.get_post_args()['params'] or 0;
end

if params ~= 0 then
  params_arr = cjson.decode(params);
  if next(params_arr) ~= nil then
    tab = params_arr.tab;
    key = params_arr.key;

    if tab=='OrgPlay' then
      local tabOrgPlay = SYNC_TAB..':'..tab..':'..key;
      --ngx.say(tabOrgPlay);
      status = instance:RPUSH(tabOrgPlay, cjson.encode(params));
      if status>0 then
        _arrayFlagKey["msgCode"]=0;
      end
    elseif tab=='OrgHall' then
      local tabOrgHall = SYNC_TAB..':'..tab;
      --ngx.say(tabOrgHall);
      status = instance:RPUSH(tabOrgHall, cjson.encode(params));
      if status>0 then
        _arrayFlagKey["msgCode"]=0;
      end
    elseif tab==nil then -- 没有传入tab的时候
      local key_lower = string.lower(key); -- key转小写

      --判断顺网
      --local key_lower_count = string.find(key_lower,"shunwang");
      --if key_lower_count==1 then
        --tab = 'ONLINE';
      --end
      --没有传入tab，直接标记ONLINE
      tab = 'ONLINE';

      if tab ~= nil then

        local mac = nil;
        local save_name = nil;
        local ctime = nil;
        local utime = nil;
        mac = params_arr.mac;
        save_name = params_arr.save_name;
        ctime = params_arr.ctime;
        utime = params_arr.utime;
        local lasttime = utime - ctime;--持续播放时间
        --ngx.say(key);
        if(key ~= nil and mac ~= nil and save_name ~= nil and ctime ~= nil and utime ~= nil and lasttime>60) then
          local KEY = SYNC_DATACENTER..':'..tab..':'..key;
          local KEYSET = mac..':'..save_name..':'..ctime;
          --ngx.say(SHUNWANG);
          --ngx.say(SHUNWANGSET);
          --ngx.say(cjson.encode(params_arr));
          status = instance:HSET(KEY, KEYSET, cjson.encode(params_arr));
          --ngx.say(status);
          if status ~= false then
            _arrayFlagKey["msgCode"]=0;
          end
        end
      end
    end
  end
end
--ngx.say(params_arr);



--ngx.say(tab);
--ngx.say(host_name);
--ngx.say(SYNC_TAB);
--ngx.say(cjson.encode(params));

instance:close();
res["res"]=0;
res["data"] = _arrayFlagKey;
ngx.say(cjson.encode(res));
